package java8Up.stream;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Fang Ruichuan
 * @date 2022-12-13 19:56
 */

public class groupDemo {
    public static void main(String[] args) {
        // 要分组的数据
        List<Map<String, Object>> courseDetail = new ArrayList<>();

        Map<String, Object> testMap1 = new LinkedHashMap<>();
        testMap1.put("moduleName", "公共基础课");
        testMap1.put("moduleLowestCredit", "7");
        testMap1.put("moduleCenterLowestCredit", "7");
        testMap1.put("gainCredit","3");
        testMap1.put("learnState", "1");
        courseDetail.add(testMap1);

        Map<String, Object> testMap2 = new LinkedHashMap<>();
        testMap2.put("moduleName", "公共基础课");
        testMap2.put("moduleLowestCredit", "7");
        testMap2.put("moduleCenterLowestCredit", "7");
        testMap2.put("gainCredit", "3");
        testMap2.put("learnState", "4");
        courseDetail.add(testMap2);

        Map<String, Object> testMap3 = new LinkedHashMap<>();
        testMap3.put("moduleName", "公共英语课");
        testMap3.put("moduleLowestCredit", "4");
        testMap3.put("moduleCenterLowestCredit", "3");
        testMap3.put("gainCredit", "4");
        testMap3.put("learnState", "4");
        courseDetail.add(testMap3);

        Map<String, Object> testMap4 = new LinkedHashMap<>();
        testMap4.put("moduleName", "公共英语课");
        testMap4.put("moduleLowestCredit", "4");
        testMap4.put("moduleCenterLowestCredit", "3");
        testMap4.put("gainCredit", "2");
        testMap4.put("learnState", "4");
        courseDetail.add(testMap4);

        System.out.println(courseDetail);

        // 多个字段分组统计处理
        Map<String, List<Map<String, Object>>> collectMap = courseDetail.stream().collect(Collectors.groupingBy(d -> d.get("moduleName") + "_" +
                d.get("moduleLowestCredit") + "_" + d.get("moduleCenterLowestCredit"), Collectors.toList()));


        // 得到分组后的集合
        List<Map<String, Object>> newCourseDetail = collectMap.keySet().stream().map(key -> {
            System.out.println("key => " + key + ", value => " + collectMap.get(key));
            Map<String, Object> pointMap = new LinkedHashMap<>();
            String[] temp = key.split("_");
            pointMap.put("moduleName", temp[0]);
            pointMap.put("moduleLowestCredit", temp[1]);
            pointMap.put("moduleCenterLowestCredit", temp[2]);

            List<Map<String, Object>> collectList = collectMap.get(key);
            if (!collectList.isEmpty()) {
                int tempCredit = 0;
                for (Map<String, Object> map: collectList) {
                    // 当条件是4时，相加分数
                    if ("4".equals(String.valueOf(map.get("learnState"))) && !String.valueOf(map.get("gainCredit")).isEmpty()) {
                        tempCredit += Integer.parseInt(String.valueOf(map.get("gainCredit")));
                    }
                    pointMap.put("gainCredit", tempCredit);
                    pointMap.put("state", tempCredit >= Integer.parseInt(temp[1]) ? "已满足毕业条件" : "未满足毕业条件");
                }
            }
            return pointMap;
        }).collect(Collectors.toList());

        System.out.println(newCourseDetail);
    }
}
